<?php

class DefaultController extends BaseController {
    
    public function actionIndex() {
        $count = Yii::app()->db->createCommand()->select('count(*) as count')->from('blog_cats')->queryRow();
        $pagination = new CPagination($count['count']);
        $pagination->setPageSize(10);
        $cats = Yii::app()->db->createCommand()->select('name, url, about, date, countComments')->from('blog_cats')->offset($pagination->getOffset())->limit($pagination->getLimit())->queryAll();
        $this->render('index', array('cats'=>$cats, 'pagination'=>$pagination));
    }
    
    public function actionCat($catUrl) {
        $catData = Yii::app()->db->createCommand()->select('name, url, about, date, catId, countComments, keywords, title, description')->from('blog_cats')->where("url=\"$catUrl\"")->queryRow();
        if(empty($catData)) throw new CHttpException(404, 'Не найдено указанной записи');
        $count = Yii::app()->db->createCommand()->select('count(*) as count')->from('blog_arts')->where('catId='.$catData['catId'])->queryRow();
        $pagination = new CPagination($count['count']);
        $pagination->setPageSize(10);
        $arts = Yii::app()->db->createCommand()->select('name, date, about, url, countComments, leaveCom')->from('blog_arts')->where('catId='.$catData['catId'])->limit($pagination->getLimit())->offset($pagination->getOffset())->queryAll();
        $this->render('cat', array('catData'=>$catData, 'arts'=>$arts, 'pagination'=>$pagination));
    }
    
    public function actionArticle($catUrl, $artUrl) {
        $model = new BlogCommentForm();
        if(isset($_POST['BlogCommentForm'])) {
            if($model->validate()) {
                Yii::app()->db->createCommand('UPDATE `blog_arts` SET `countComments` = `countComments` + 1 WHERE `artId`='.$_POST['BlogCommentForm']['artId'])->query();
                Yii::app()->db->createCommand('UPDATE `blog_cats` SET `countComments` = `countComments` + 1 WHERE `catId`='.$_POST['BlogCommentForm']['catId'])->query();
                Yii::app()->db->createCommand()->insert('blog_comments', array('userId'=>Yii::app()->user->getId(), 'text'=>$model->text, 'date'=>time(), 'artId'=>$_POST['BlogCommentForm']['artId']));
                $this->refresh();
            }
        }
        $data = Yii::app()->db->createCommand()->select('a.text, a.date, a.name, a.title, a.description, a.url, a.keywords, a.countComments, a.leaveCom, a.artId, c.catId, c.name as catName, c.name as catName')->from('blog_arts a')->join('blog_cats c', 'c.catId=a.catId and c.url="'.$catUrl.'"')->where("a.url=\"$artUrl\"")->queryRow();
        if(empty($data)) throw new CHttpException(404, 'Не найдено указанной записи');
        if($data['leaveCom']) $com = Yii::app()->db->createCommand()->select('c.text, c.date, u.login')->from('blog_comments c')->join('users u', 'u.userid=c.userId')->where('artId="'.$data['artId'].'"')->queryAll();
        $this->render('article', array('model'=>$model, 'data'=>$data, 'com'=>!empty($com) ? $com : ''));
    }
}